import { createRouter, createWebHistory } from 'vue-router'

// 导入路由模块
import hrRoutes from './modules/hr'
// 导入其他模块路由...

// 在文件顶部引入公告路由模块
import announcementRouter from './modules/announcement'

/**
 * 注意：子菜单只在路由children.length >= 1时出现
 * 详情见: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果设置为true，项目将不会显示在侧边栏中（默认为false）
 * alwaysShow: true               如果设置为true，将始终显示根菜单
 *                                如果不设置alwaysShow，当项目有多个子路由时，
 *                                它将变成嵌套模式，否则不显示根菜单
 * redirect: noRedirect           如果设置noRedirect将不会在面包屑中重定向
 * name:'router-name'             该名称由<keep-alive>使用（必须设置!!!）
 * meta : {
    roles: ['admin','editor']    控制页面角色（可以设置多个角色）
    title: 'title'               侧边栏和面包屑中显示的名称（推荐设置）
    icon: 'svg-name'/'el-icon-x' 侧边栏中显示的图标
    breadcrumb: false            如果设置为false，则该项将隐藏在面包屑中（默认为true）
    activeMenu: '/example/list'  如果设置路径，侧边栏将突出显示您设置的路径
  }
 */

/**
 * constantRoutes
 * 没有权限要求的基本页面
 * 所有角色都可以访问
 */
const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: () => import('@/layout/index'),
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
]

/**
 * asyncRoutes
 * 需要根据用户角色动态加载的路由
 */
const asyncRoutes = [
  hrRoutes,
  {
    path: '/document',
    component: () => import('@/layout/index'),
    redirect: '/document/list',
    meta: { title: '公文管理', icon: 'document' },
    children: [
      {
        path: 'list',
        name: 'DocumentList',
        component: () => import('@/views/document/list'),
        meta: { title: '公文列表' }
      },
      {
        path: 'create',
        name: 'DocumentCreate',
        component: () => import('@/views/document/create'),
        meta: { title: '新建公文' }
      },
      {
        path: 'approval',
        name: 'DocumentApproval',
        component: () => import('@/views/document/approval'),
        meta: { title: '公文审批' }
      }
    ]
  },

  {
    path: '/workflow',
    component: () => import('@/layout/index'),
    redirect: '/workflow/process',
    meta: { title: '工作流管理', icon: 'workflow' },
    children: [
      {
        path: 'process',
        name: 'ProcessDesign',
        component: () => import('@/views/workflow/process'),
        meta: { title: '流程设计' }
      },
      {
        path: 'template',
        name: 'ProcessTemplate',
        component: () => import('@/views/workflow/template'),
        meta: { title: '流程模板' }
      },
      {
        path: 'monitor',
        name: 'ProcessMonitor',
        component: () => import('@/views/workflow/monitor'),
        meta: { title: '流程监控' }
      }
    ]
  },

  {
    path: '/system',
    component: () => import('@/layout/index'),
    redirect: '/system/user',
    meta: { title: '系统管理', icon: 'setting' },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/system/user/index'),
        meta: { title: '用户管理' }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/system/role/index'),
        meta: { title: '角色管理' }
      },
      {
        path: 'department',
        name: 'Department',
        component: () => import('@/views/system/department/index'),
        meta: { title: '部门管理' }
      }
    ]
  },

  announcementRouter,

  // 404页必须放在最后!!!
  { path: '/:pathMatch(.*)*', redirect: '/404', hidden: true }
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes.concat(asyncRoutes)
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem('token')
  
  if (to.path === '/login') {
    next()
  } else {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router